<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2019/5/4
 * Time: 21:36
 */

namespace app\storage\controller;

use cmf\controller\AdminBaseController;
use app\storage\model\InflowModel;
use app\storage\model\OutflowModel;
use think\Request;
use app\storage\service\CommonService;

class ChartsController extends AdminBaseController
{
    private $commonService;
    private $inflowModel;
    private $outflowModel;
    use \app\storage\util\Common;

    public function __construct(CommonService $commonService, InflowModel $inflowModel, OutflowModel $outflowModel, Request $request = null)
    {
        parent::__construct($request);
        $this->inflowModel = $inflowModel;
        $this->outflowModel = $outflowModel;
        $this->commonService = $commonService;
    }

    public function index()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('index');
    }

    public function day()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('day');
    }

    public function subMateriel()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = $this->commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        $this->assign($assign);
        return $this->fetch('subMateriel');
    }


    /**
     * @return mixed 按传入的天数获取入库件数
     * $dayNum 天数
     * $storageUuid 仓库的uuid
     * return 这几天中指定仓库每一天入库的件数
     * 用于折线图
     */
    public function getInflowByDay()
    {
        $storageUuid = input('storageUuid');
        $dayNum = input('dayNum');
        if (!empty($storageUuid) && $storageUuid != "undefined") {
            $where['storage_uuid'] = $storageUuid;//若传入正确非空的storageUuid则添加搜索仓库的条件。
        }
        for ($i = 0; $i < $dayNum; $i++) {
            //$dayNum-($i+1)指的是n天前，比如$i = 0代表第一天，一共有$dayNum = 7天，则$dayNum-($i+1) = 7-1=6天。strtotime("-6 day")获取6天前的时间。
            $time = date("Y-m-d", strtotime('-' . ($dayNum - ($i + 1)) . 'day'));
            $where['time'] = array('between time', [$time . ' 00:00:00', $time . ' 23:59:59']);
            $result[$i] = $this->inflowModel->getInflowNum($where);
        }
        return $result;
    }

    /**
     * @return mixed 按传入的天数获取出库件数
     * $dayNum 天数
     * $storageUuid 仓库的uuid
     * return 这几天中指定仓库每一天出库的件数
     * 用于折线图
     */
    public function getOutflowByDay()
    {
        $storageUuid = input('storageUuid');//仓库uuid
        $dayNum = input('dayNum');
        if (!empty($storageUuid) && $storageUuid != "undefined") {//若传入正确非空的storageUuid则添加搜索仓库的条件。
            $where['storage_uuid'] = $storageUuid;
        }

        for ($i = 0; $i < $dayNum; $i++) {
            //$dayNum-($i+1)指的是n天前，比如$i = 0代表第一天，一共有$dayNum = 7天，则$dayNum-($i+1) = 7-1=6天。strtotime("-6 day")获取6天前的时间。
            $time = date("Y-m-d", strtotime('-' . ($dayNum - ($i + 1)) . 'day'));
            $where['time'] = array('between time', [$time . ' 00:00:00', $time . ' 23:59:59']);
            $result[$i] = $this->outflowModel->getOutflowNum($where);
        }
        return $result;
    }

    /**按传入的天数和仓库uuid获取出库物料件数前十
     * @return \think\response\Json
     */
    public function getOutflowMaterielByDay()
    {
        $storageUuid = input('storageUuid');//仓库uuid
        $dayNum = input('dayNum');
        if (!empty($storageUuid) && $storageUuid != "undefined") {//若传入正确非空的storageUuid则添加搜索仓库的条件。
            $where['storage_uuid'] = $storageUuid;
        }

        //假设dayNum为3天，-($dayNum-1)指2天前00:00:00算起，再加上今天这一天一共3天
        $time = date("Y-m-d", strtotime('-' . ($dayNum - 1) . 'day'));
        $where['time'] = array('between time', [$time . ' 00:00:00', date("Y-m-d H:i:s", strtotime('now'))]);
        $outflow_data = $this->outflowModel->getOutflowMaterielNum($where)->toArray();
        $result = array_column($outflow_data, 'num', 'sub_materiel_id');
        array_multisort($result);
        return json($result);

    }

    /**按传入的天数和仓库uuid获取入库物料件数前十
     * @return array
     */
    public function getInflowMaterielByDay()
    {
        $storageUuid = input('storageUuid');//仓库uuid
        $dayNum = input('dayNum');
        if (!empty($storageUuid) && $storageUuid != "undefined") {//若传入正确非空的storageUuid则添加搜索仓库的条件。
            $where['storage_uuid'] = $storageUuid;
        }

        //假设dayNum为3天，-($dayNum-1)指2天前00:00:00算起，再加上今天这一天一共3天
        $time = date("Y-m-d", strtotime('-' . ($dayNum - 1) . 'day'));
        $where['time'] = array('between time', [$time . ' 00:00:00', date("Y-m-d H:i:s", strtotime('now'))]);
        $inflow_data = $this->inflowModel->getInflowMaterielNum($where)->toArray();
        $result = array_column($inflow_data, 'num', 'sub_materiel_id');
        array_multisort($result);
        return $result;

    }

    /***获取根据年份获取每一天出库数量
     * @return mixed
     * 数据过多，性能无法达到要求，因此废弃
     * 暂时先放着
     */
    public function getOutflowByYear()
    {
        /*因为一个月的有多少天改成index.html里面判断，
        所以这边默认设置31天就可以，
        不存在的日期如2月31日会查出0，之后在网页上会筛掉这些日期
        之前的getInflowByYear的天数判断变得多余了，不过也没差别*/
        $year = input('year');
        for ($i = 1; $i <= 12; $i++) {
            for ($j = 1; $j <= 31; $j++) {
                $beginTime = $year . '-' . $i . '-' . $j . ' 00:00:00';
                $endTime = $year . '-' . $i . '-' . $j . ' 23:59:59';
                $where['time'] = array('between time', [$beginTime, $endTime]);
                $result[$i][$j] = $this->outflowModel->getOutflowNum($where);
            }
        }
        return $result;
    }

    /**传入年份，将该年每一天的入库件数获取出来
     * @return mixed
     * * 数据过多，性能无法达到要求，因此废弃
     * 暂时先放着吧
     */
    public function getInflowByYear()
    {
        $year = input('year');
        global $result;
        for ($i = 1; $i <= 12; $i++) {
            //判断是闰年还是平年
            //day_num指的是每个月的天数，因为day[1]指的是1月的天数，所以day_num[0]要加一个占位，实际上并没有用到。
            if (intval($year) % 4 == 0 && intval($year) % 100 != 0 || intval($year) % 400 == 0) {
                $day_num = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];//每个月的天数
            } else {
                $day_num = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            }
            for ($j = 1; $j <= $day_num[$i]; $j++) {
                $beginTime = $year . '-' . $i . '-' . $j . ' 00:00:00';
                $endTime = $year . '-' . $i . '-' . $j . ' 23:59:59';
                $where['time'] = array('between time', [$beginTime, $endTime]);
                $result[$i][$j] = $this->inflowModel->getInflowNum($where);//获取每一天的数量
            }

        }
        return $result;
    }


}
